package com.chenyun.rpc.dubbo.satoken.model;

import cn.dev33.satoken.context.model.SaStorage;
import org.apache.dubbo.rpc.RpcContext;

/**
 * 移植 sa-token-dubbo3 中的代码到此处
 * @author: Lonni
 * @date: 2024/1/22 0022 16:35
 */
public class SaStorageForDubbo3 implements SaStorage {
    protected RpcContext rpcContext;

    public SaStorageForDubbo3(RpcContext rpcContext) {
        this.rpcContext = rpcContext;
    }

    public Object getSource() {
        return this.rpcContext;
    }

    public SaStorageForDubbo3 set(String key, Object value) {
        this.rpcContext.setObjectAttachment(key, value);
        if (key.equals("JUST_CREATED_NOT_PREFIX_"))
            RpcContext.getServerContext().setAttachment(key, value);
        return this;
    }

    public Object get(String key) {
        return this.rpcContext.getObjectAttachment(key);
    }

    public SaStorageForDubbo3 delete(String key) {
        this.rpcContext.removeAttachment(key);
        return this;
    }
}



